/**   
* @Title: GuardianImpl.java 
* @Package cn.hdmis.dao.imp 
* @Description: TODO
* @author pnwazgx@163.com   
* @date 2011-3-26 下午11:01:54 
* @version V1.0   
*/
package cn.hdmis.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import cn.hdmis.dao.GuardianDao;
import cn.hdmis.entity.Guardian;
import cn.hdmis.entity.Student;
import cn.hdmis.util.HibernateUtil;

/** 
 * @ClassName: GuardianImpl 
 * @Description: TODO
 * @author pnwzgx@163.com
 * @date 2011-3-26 下午11:01:54 
 *  
 */

public class GuardianDaoImpl extends BaseDaoImp<Guardian> implements GuardianDao {

	/* (非 Javadoc) 
	* <p>Title: addGuardian</p> 
	* <p>Description: </p>  
	* @see cn.hdmis.dao.GuardianDao#addGuardian() 
	*/
	@Override
	public void addGuardian(Guardian guardian) {
		super.save(guardian);
		
	}

	/* (非 Javadoc) 
	* <p>Title: addGuardian</p> 
	* <p>Description: </p> 
	* @param guardians 
	* @see cn.hdmis.dao.GuardianDao#addGuardian(java.util.List) 
	*/
	@Override
	public void addGuardian(List<Guardian> guardians) {
		// TODO Auto-generated method stub
		for (Guardian guardian : guardians) {
			addGuardian(guardian);
		}
	}
	/* (非 Javadoc) 
	* <p>Title: addGuardian</p> 
	* <p>Description: </p> 
	* @param guardians 
	* @see cn.hdmis.dao.GuardianDao#addGuardian(java.util.List) 
	*/
	@Override
	public void updateGuardian(Guardian guardian) {
		// TODO Auto-generated method stub
		update(guardian);
	}
	/* (非 Javadoc) 
	* <p>Title: findByStudent</p> 
	* <p>Description: </p> 
	* @param stu 
	* @see cn.hdmis.dao.GuardianDao#findByStudent(cn.hdmis.entity.Student) 
	*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Guardian> findByStudent(Student stu) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Guardian.class);
		criteria.add(Restrictions.eq("student_id", stu.getId()));
		return criteria.list();
		
	}

	/* (非 Javadoc) 
	* <p>Title: removeGuardian</p> 
	* <p>Description: </p> 
	* @param student 
	* @see cn.hdmis.dao.GuardianDao#removeGuardian(cn.hdmis.entity.Student) 
	*/
	@Override
	public void removeGuardian(Student student) {
		Set<Guardian> gset = student.getGuardiansSet();
		// TODO Auto-generated method stub
		for (Guardian object : gset) {
			del(object.getId()) ;
		}		
	}

	/* (非 Javadoc) 
	* <p>Title: removeGuardian</p> 
	* <p>Description: </p> 
	* @param id 
	* @see cn.hdmis.dao.GuardianDao#removeGuardian(java.io.Serializable) 
	*/
	@Override
	public void removeGuardian(Serializable id) {
		// TODO Auto-generated method stub
		del(id);
	}
	
	/* (非 Javadoc) 
	* <p>Title: removeGuardian</p> 
	* <p>Description: </p> 
	* @param id 
	* @see cn.hdmis.dao.GuardianDao#removeGuardian(java.util.List) 
	*/
	@Override
	public void removeGuardian(List<Integer> idList) {
		// TODO Auto-generated method stub
		for (Integer id : idList) {
			del(id);
		}
	}

	public void del(Serializable id){
		HibernateUtil.beginTransaction();
		Query query = this.getSession().createQuery("update Guardian set status =:status where id =:id");
		query.setBoolean("status", false);
		query.setParameter("id", id);
		query.executeUpdate();
		HibernateUtil.commitTransaction();
	}

}
